package zj.com.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.List;

import zj.com.activities.DshDetailEditActivity;
import zj.com.activities.DzgDetailEditActivity;
import zj.com.app.ZjApplication;
import zj.com.db.DBUtil;
import zj.com.interfaze.CallData;
import zj.com.model.Dic;
import zj.com.model.DjcModel;
import zj.com.model.DzgInspectTask;
import zj.com.util.Constants;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class DshDetailAdapter extends BaseAdapter{

    public List<DzgInspectTask> list;
    public Context context;
    public LayoutInflater inflater;
    DbManager dbManager;
    public DshDetailAdapter(List<DzgInspectTask> list, Context context){
         dbManager= DBUtil.getInstance(context).getDBManager();

        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_dsh_detail_item, null);
            viewHolder.lookimg_tv = (TextView) convertView.findViewById(R.id.lookimg_tv);
            viewHolder.update_tv = (TextView) convertView.findViewById(R.id.update_tv);
            viewHolder.reject_tv = (TextView) convertView.findViewById(R.id.reject_tv);
            viewHolder.name_tv = (TextView) convertView.findViewById(R.id.name_value_tv);
            viewHolder.desc_tv = (TextView) convertView.findViewById(R.id.desc_value_tv);
            viewHolder.fen_tv = (TextView) convertView.findViewById(R.id.fen_value_tv);
            viewHolder.result_tv = (TextView) convertView.findViewById(R.id.result_value_tv);
            viewHolder.risk_value_tv = (TextView) convertView.findViewById(R.id.risk_value_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }



        final DzgInspectTask task = list.get(position);
        viewHolder.name_tv.setText(task.getInspectContentDesc());
        viewHolder.desc_tv.setText(task.getInspectRiskDesc());
        viewHolder.fen_tv.setText(task.getPoints()+"");
        try {
            Dic dic = dbManager.selector(Dic.class).where("dic_value", "=", task.getInspectContentStatus() + "").and("dic_type", "=", Constants.RepairRecordState).findFirst();
            if(dic != null){
                viewHolder.result_tv.setText(dic.getName());
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

        viewHolder.risk_value_tv.setText(task.getRiskTypeName());


        viewHolder.lookimg_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            Intent intent =     new Intent(context, DshDetailEditActivity.class);
                intent.putExtra("task",task);
            context.startActivity(intent);
            }
        });

        viewHolder.update_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(data != null){

                    data.call(task,0+"");
                }
            }
        });

        viewHolder.reject_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(data != null){

                    data.call(task,1+"");
                }
            }
        });

        return convertView;
    }
    private static class ViewHolder {
        TextView lookimg_tv;
        TextView update_tv;
        TextView name_tv;
        TextView desc_tv;
        TextView fen_tv;
        TextView result_tv;
        TextView reject_tv;
        TextView risk_value_tv;

    }

    public CallData data;
    public void setDataChange(List<DzgInspectTask> list){

        this.list  = list;
        notifyDataSetChanged();
    }

    public void setCallData(CallData data){

        this.data = data;
        notifyDataSetChanged();
    }
}
